package cn.lyh97.estore.easystoremanager.Fragments;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

import cn.lyh97.estore.easystoremanager.R;

public class ChartFragment extends Fragment {
    private PieChart mChart;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View chartFragmentView = inflater.inflate(R.layout.fragment_chart, container, false);

//        setChartSpinner(chartFragmentView);

        mChart = ( PieChart ) chartFragmentView.findViewById(R.id.chartFragmentChartView);

        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
        entries.add(new PieEntry(100.f, "test"));
 /*       entries.add(new PieEntry(18.0f, "鞋子"));
        entries.add(new PieEntry(34.0f, "衣服"));
        entries.add(new PieEntry(38.0f, "裤子"));*/
        setPieData(mChart,entries);
        initChart(mChart,"请选择比例图");
        return chartFragmentView;
    }

    public static void initChart(PieChart chart,String centerText){
        chart.setHoleRadius(60f);  //半径
        chart.setTransparentCircleRadius(64f); // 半透明圈
//        Description description = new Description();
//        description.setText("EasyStoreManager");
//        chart.setDescription(description);
        chart.setDescription(null);
        chart.setRotationAngle(0); // 初始旋转角度
        chart.setRotationEnabled(true); // 可以手动旋转
        chart.setUsePercentValues(true);  //显示成百分比
        chart.setCenterText(centerText);  //饼状图中间的文字
        Legend mLegend = chart.getLegend();  //设置比例图
        mLegend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);  //最右边显示
//      mLegend.setForm(LegendForm.LINE);  //设置比例图的形状，默认是方形
        mLegend.setXEntrySpace(7f);
        mLegend.setYEntrySpace(5f);
        chart.animateXY(1000, 1000);  //设置动画
    }

    public static void setPieData(PieChart chart, ArrayList<PieEntry> entries) {

//        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();

        /**饼图数据
         * 将一个饼形图分成四部分， 四部分的数值比例为14:14:34:38
         * 所以 14代表的百分比就是14%
         */
//        float quarterly1 = 14;
//        float quarterly2 = 14;
//        float quarterly3 = 34;
//        float quarterly4 = 38;
//
//        entries.add(new PieEntry(quarterly1, "其他"));
//        entries.add(new PieEntry(quarterly2, "鞋子"));
//        entries.add(new PieEntry(quarterly3, "衣服"));
//        entries.add(new PieEntry(quarterly4, "裤子"));

        PieDataSet dataSet = new PieDataSet(entries,null);
        dataSet.setSliceSpace(3f); //设置个饼状图之间的距离
        dataSet.setSelectionShift(5f);
        // 饼图颜色
        ArrayList<Integer> colors = new ArrayList<Integer>();
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);
        colors.add(ColorTemplate.getHoloBlue());

        dataSet.setColors(colors);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());

        data.setValueTextSize(11f);
        data.setValueTextColor(Color.GRAY);
        chart.setEntryLabelTextSize(15f);
        chart.setEntryLabelColor(Color.BLACK);

        chart.setData(data);
        // undo all highlights
        chart.highlightValues(null);
        chart.invalidate();
    }

/*    public void setChartSpinner(View view){
        Spinner spinner = (Spinner) view.findViewById(R.id.chartFragmentTagSpinner);

*//*        List<GoodsItem> goodsList = sqlHelper.getAllGoodsItem();
        ArrayList<String> listStr = new ArrayList<String>();

        for(int i = 0; i < goodsList.size(); i ++){
            String tag = goodsList.get(i).getTag();
            if( !listStr.contains(tag) ){
                listStr.add(tag);
            }
        }
        String[] strings = new String[listStr.size()];
        for(int i = 0; i < listStr.size(); i ++){
            strings[i] = listStr.get(i);
        }*//*

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_selectable_list_item,new String[]{"库存数量比","销售利润比"});

        spinner.setAdapter(adapter);

    }*/
}
